<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class CreateUsersTable extends Migration
{
    /**
     * 数据表名称
     * @var string
     */
    private $tableName = 'users';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建数据表
        Schema::create($this->tableName, function (Blueprint $table) {
            // 数据表类型
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

            // 数据表字段
            $table->bigIncrements('user_id')->comment('用户主键');
            $table->string('phone', 20)->nullable()->comment('手机号码');
            $table->string('name', 20)->default('')->comment('用户名称');
            $table->string('number', 20)->nullable()->comment('会员号')->uniqid();
            $table->string('avatar', 200)->nullable()->comment('用户头像');
            $table->string('remarks', 200)->nullable()->comment('用户备注');
            $table->integer('group_id', false, true)->nullable()->comment('权限分组');
            $table->tinyInteger('is_admin')->default(0)->comment('后台管理 0:否 1:是');
            $table->tinyInteger('status', false, true)->default(1)->comment('可用状态 0:停用 1:正常');
            $table->dateTime('deleted_at')->nullable()->comment('数据删除时间');
            $table->dateTime('created_at')->comment('数据创建时间');
            $table->timestamp('updated_at')->comment('最后更新时间')->default(DB::raw('CURRENT_TIMESTAMP'));

            // 数据表索引
            $table->unique('user_id');
            $table->unique('phone');
        });
        // 设置数据表备注信息
        DB::statement("ALTER TABLE `".env('DB_PREFIX')."{$this->tableName}` COMMENT'系统用户表'");
        DB::statement("ALTER TABLE `".env('DB_PREFIX')."{$this->tableName}` AUTO_INCREMENT=30100");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
